package com.fuyu.system.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fuyu.system.dao.SysUserDao;
import com.fuyu.system.domain.SecurityUser;
import com.fuyu.system.domain.UserInfo;
import com.fuyu.system.entity.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

@Service
public class MyUserDetailsService implements UserDetailsService {

    @Autowired
    private SysUserDao sysUserDao;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

        SysUser sysUser = sysUserDao.selectOne(
                new LambdaQueryWrapper<SysUser>()
                        .eq(SysUser::getUsername, username)
        );
        UserInfo loginUser = UserInfo.builder()
                .userId(String.valueOf(sysUser.getUserId()))
                .username(sysUser.getUsername())
                .password(sysUser.getPassword())
                .build();
        return new SecurityUser(loginUser, new ArrayList<>(), null);
    }
}